package com.steven.im.ui.fragment.transfragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.steven.putorefresh.BaseBothListener;
import com.steven.putorefresh.PullToBothLayout;
import com.steven.im.R;
import com.steven.im.adapter.MyWalletAdapter;
import com.steven.im.base.Constant;
import com.steven.im.base.LazyLoadBaseFragment;
import com.steven.im.entity.BaseEntity;
import com.steven.im.entity.PageEntity;
import com.steven.im.entity.WalletEntity;
import com.steven.im.net.MyCallback;
import com.steven.im.net.Task;
import com.steven.im.utils.GsonUtils;
import com.steven.im.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Response;

/**
 * Created by Steven on 2019/1/11.
 * 收入
 */
public class IncomeDetailsFragment extends LazyLoadBaseFragment {

    PullToBothLayout pullToBothLayout;
    private boolean isRefresh = false;//是否是下拉刷新
    private boolean isLoadMore = false;//是否是 上拉加载
    int totalPage = 0;//总页数

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    MyWalletAdapter mAdapter;
    List<WalletEntity> mList = new ArrayList<>();

    int pageNo = 1;

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_transaction_details;
    }

    @Override
    protected void initView(View rootView) {

        if (pullToBothLayout == null) {
            pullToBothLayout = rootView.findViewById(R.id.pullRefreshBoth);
        }

        //列表
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new MyWalletAdapter(mList);
        recyclerView.setAdapter(mAdapter);

        setListener();
    }

    /**
     * 事件监听
     */
    private void setListener() {
        pullToBothLayout.setRefreshListener(new BaseBothListener() {
            @Override
            public void refresh() {
                isRefresh = true;
                pageNo = 1;
                requestData();
            }

            @Override
            public void loadMore() {
                if (pageNo < totalPage) {
                    isLoadMore = true;
                    pageNo++;
                    requestData();
                } else {
                    ToastUtil.showShort(getActivity(), "数据加载完毕");
                    pullToBothLayout.finishLoadMore();
                }

            }
        });
    }

    @Override
    protected void requestData() {
        super.requestData();
        getIncomeData(pageNo);
    }

    /**
     * 获取收入数据
     */
    private void getIncomeData(int pageNo) {
        Task.getApiService().getMyWallet(pageNo, Constant.CommonInfo.PAGE_SIZE, 1)//（0支出，1，收入）
                .enqueue(new MyCallback<BaseEntity>(getActivity()) {
                    @Override
                    protected void onSuccess(Response<BaseEntity> response) {
                        try {
                            pullToBothLayout.finishRefresh();
                            pullToBothLayout.finishLoadMore();
                            if ("0".equals(response.body().getCode())) {
                                List<WalletEntity> walletList = GsonUtils.jsonToArrayList(
                                        response.body().getResult().getJSONArray("tradeDetailList").toString(),
                                        WalletEntity.class
                                );
                                if (walletList != null) {
                                    if (isRefresh) {//刷新
                                        isRefresh = false;
                                        mList.clear();
                                    } else if (isLoadMore) {//加载
                                        isLoadMore = false;
                                    } else {
                                        mList.clear();
                                    }
                                    mList.addAll(walletList);
                                    mAdapter.setNewData(mList);
                                }

                                //分页
                                PageEntity pageEntity = GsonUtils.parseJSON(response.body().getResult()
                                        .getJSONObject("pager").toString(), PageEntity.class);
                                if (pageEntity != null) {
                                    totalPage = Integer.valueOf(pageEntity.getTotalPage());
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(Throwable t) {
                        pullToBothLayout.finishRefresh();
                        pullToBothLayout.finishLoadMore();
                    }
                });
    }

}
